package cn.fastoo.common.dao.product.query;
import cn.fastoo.common.dao.product.ProductCategoryDao;
import cn.fastoo.common.entity.product.ProductCategory;
import cn.fastoo.common.entity.product.Query.ViewProductCategory;
import cn.fastoo.common.entity.product.Query.ViewProductCategoryAttrgroup;
import cn.fastoo.common.entity.product.Query.ViewProductCategoryAttrgroupAttr;
import cn.fastoo.common.entity.product.Query.ViewProductCategoryAttrgroupAttrValue;
import cn.fastoo.common.sampleframe.SFBeanUtils;
import cn.fastoo.common.sampleframe.dao.FastDao;
import cn.fastoo.common.sampleframe.dao.impl.FastDaoImpl;
import cn.fastoo.common.sampleframe.dto.JsonResult;
import cn.fastoo.common.sampleframe.search.Search;
import cn.fastoo.common.sampleframe.springside.Collections3;
import cn.hutool.http.HttpUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.sql.DataSource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;


@Component
public class ViewProductCategoryDao extends FastDaoImpl<ViewProductCategory> implements FastDao<ViewProductCategory> {

    @Autowired
    @Qualifier("mainDS")
    private void setJt(DataSource ds){
        this.jt = new JdbcTemplate(ds);
    }

    public Logger logger = LoggerFactory.getLogger(ViewProductCategoryDao.class);

    @Autowired
    private ProductCategoryDao productCategoryDao;
    @Autowired
    private ViewProductCategoryAttrgroupDao groupDao;
    @Autowired
    private ViewProductCategoryAttrgroupAttrDao attrDao;
    @Autowired
    private ViewProductCategoryAttrgroupAttrValueDao valueDao;


    private Map<Long,ViewProductCategory> cateMap;
    private Map<Long,List<ViewProductCategoryAttrgroupAttr>> saleAttrMap;

    @Scheduled(cron = "0/15 * * * * ?")
    private void init(){
        //找到所有的Category

        List<ViewProductCategoryAttrgroupAttrValue> values = valueDao.find(new Search());
        if(Collections3.isEmpty(values)){
            return;
        }
        List<ViewProductCategoryAttrgroupAttr> attrs = attrDao.find(new Search());
        if(Collections3.isEmpty(attrs)){
            return;
        }
        Map<Long,List<ViewProductCategoryAttrgroupAttrValue>> valueByAttrId = values.stream().collect(Collectors.groupingBy(ViewProductCategoryAttrgroupAttrValue::getProductCategoryAttrgroupAttrId));
        attrs.forEach(t->{
            t.setValues(valueByAttrId.get(t.getId()));
        });

        Map<Long,List<ViewProductCategoryAttrgroupAttr>> attrByGroupId = attrs.stream().collect(Collectors.groupingBy(ViewProductCategoryAttrgroupAttr::getProductCategoryAttrgroupId));

        List<ViewProductCategoryAttrgroup> groups = groupDao.find(new Search());
        if(Collections3.isEmpty(groups)){
            return;
        }
        groups.forEach(t->{
            t.setAttrs(attrByGroupId.get(t.getId()));
        });
        Map<Long,List<ViewProductCategoryAttrgroup>> groupByCategoryId = groups.stream().collect(Collectors.groupingBy(ViewProductCategoryAttrgroup::getProductCategoryId));


        List<ViewProductCategory> categoryList = find(new Search());
        if(Collections3.isEmpty(categoryList)){
            return;
        }
        categoryList.forEach(t->{
            t.setGroups(groupByCategoryId.get(t.getId()));
        });

        cateMap = categoryList.stream().collect(Collectors.toMap(ViewProductCategory::getId, Function.identity()));

        initSaleAttrMap();
    }
    public void initSaleAttrMap(){
        try {
            if(cateMap!=null && cateMap.size()>0){
                for (ViewProductCategory vpc:cateMap.values()) {
                    List<ViewProductCategoryAttrgroupAttr> attrs = new ArrayList<>();
                    if(Collections3.isNotEmpty(vpc.getGroups())){
                        vpc.getGroups().forEach(t->{

                            if(Collections3.isNotEmpty(t.getAttrs())){
                                t.getAttrs().forEach(attr->{
                                    if(attr.getIsSale().equals(1)){
                                        attrs.add(attr);
                                    }
                                });
                            }

                        });
                    }

                    if(Collections3.isNotEmpty(attrs)){
                        if(saleAttrMap==null){
                            saleAttrMap = new HashMap<>();
                        }
                        saleAttrMap.put(vpc.getId(),attrs);
                    }
                }
            }
        }catch (Exception e){
            e.printStackTrace();
        }

    }


    public ViewProductCategory getById(Long id){
        return cateMap.get(id);
    }
    public List<ViewProductCategoryAttrgroupAttr> getSaleAttrById(Long id){
        return saleAttrMap.get(id);
    }



}
